package com.wuxiaofan.api;

import com.alibaba.fastjson.JSONObject;
import com.wuxiaofan.entity.ProductInfo;
import com.wuxiaofan.service.ProductInfoService;
import com.wuxiaofan.vo.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("productInfo")
public class ProductInfoController {

    @Autowired
    private ProductInfoService productInfoService;

    @RequestMapping("subRepertory")
    public ReturnResult subRepertory(@RequestBody JSONObject jsonObject) throws Exception{
        String id = jsonObject.getString("id");
        int num = jsonObject.getIntValue("num");
        ProductInfo productInfo = productInfoService.findById(id);
        if(productInfo.getRepertory()>=num){
            productInfoService.subRepertory(id,num);
        }else{
            throw new Exception("库存不足");
        }
        return ReturnResult.success("扣库存成功");
    }
}
